package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.dto.AclModuleLevelDto;
import com.aabte.permissionframeworklearn.permission.model.SysAclModule;
import com.aabte.permissionframeworklearn.permission.param.AclModuleParam;
import com.aabte.permissionframeworklearn.permission.service.SysAclModuleService;
import com.aabte.permissionframeworklearn.permission.service.SysTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Daniel
 */
@Controller
@RequestMapping("/sys/aclModule")
public class SysAclModuleController {

    @Autowired
    private SysAclModuleService sysAclModuleService;

    @Autowired
    private SysTreeService sysTreeService;

    @GetMapping("/acl.page")
    public ModelAndView page() {
        return new ModelAndView("acl");
    }

    @PostMapping("/save.json")
    @ResponseBody
    public ResponseBean saveAclModule(
            @RequestBody AclModuleParam param
    ) {
        SysAclModule save = sysAclModuleService.save(param);
        return ResponseBean.success(save);
    }

    @PutMapping("/update.json")
    @ResponseBody
    public ResponseBean updateAclModule(
            @RequestBody AclModuleParam param
    ) {
        SysAclModule update = sysAclModuleService.update(param);
        return ResponseBean.success(update);
    }

    @GetMapping("/tree.json")
    @ResponseBody
    public ResponseBean tree() {
        List<AclModuleLevelDto> aclModuleTree = sysTreeService.aclModuleTree();
        return ResponseBean.success(aclModuleTree);
    }

    @DeleteMapping("/delete.json")
    @ResponseBody
    public ResponseBean deleteAclModule(
            @RequestParam("id") Long id
    ) {
        SysAclModule delete = sysAclModuleService.delete(id);
        return ResponseBean.success(delete);
    }

}
